/**   
 * Copyright © 2016 北京之洲科技有限责任公司. All rights reserved.
 * 
 * @Title: CarInfoCrawlOfAutoHome.java 
 * @Prject: carfactory
 * @Package: com.zizo.autohome.carinfo.crawl 
 * @Description: 
 * @author: Alexis  
 * @email heyan0227@outlook.com
 * @date: 2016年8月11日 下午4:39:26 
 * @version: V1.0   
 */
package com.zizo.autohome.carinfo.crawl;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.zizo.autohome.carinfo.bean.AutoHomeCarInfo;
import com.zizo.autohome.carinfo.dao.CarInfoDaoOfAutoHome;
import com.zizo.core.Crawl;
import com.zizo.core.DAO;
import com.zizo.core.Errors;
import com.zizo.util.ConnectionPool;

/**
 * @ClassName: CarInfoCrawlOfAutoHome
 * @Description:
 * @author: Alexis
 * @date: 2016年8月11日 下午4:39:26
 */
public class CarInfoCrawlOfAutoHome extends Crawl {
	CarInfoDaoOfAutoHome carinfodao;

	/**
	 * @Title:CarInfoCrawlOfAutoHome
	 * @Description:
	 * @param id
	 * @param dao
	 */
	public CarInfoCrawlOfAutoHome(String id, DAO dao) {
		super(id, dao);
	}

	public CarInfoCrawlOfAutoHome(String id, DAO dao, List<Errors> error, CarInfoDaoOfAutoHome carinfodao) {
		super(id, dao, error);
		this.carinfodao = carinfodao;
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getPages
	 * 
	 * @Description:
	 * 
	 * @param doc
	 * 
	 * @param selectStr
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.CrawlInterface#getPages(org.jsoup.nodes.Document,
	 * java.lang.String)
	 */
	@Override
	public int getPages(Document doc, String selectStr) {
		return 0;
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getItems
	 * 
	 * @Description:
	 * 
	 * @param url
	 * 
	 * @see com.zizo.core.CrawlInterface#getItems(java.lang.String)
	 */
	@Override
	public void getItems(String url) {
		// url = http://car.autohome.com.cn/javascript/NewSpecCompare.js?;
		Document doc = null;
		try {
			doc = Jsoup.parse(new File("/var/server/log/lisongyuan/carinfo.js"), "utf-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		String json = doc.text().substring(21, doc.text().length() - 1);
		try {
			JSONArray jsonarray = new JSONArray(json);
			for (int i = 0; i < jsonarray.length(); i++) {
				JSONObject jsonObj = jsonarray.getJSONObject(i);
				int brand_id = jsonObj.getInt("I");
				String brand_firstletter = jsonObj.getString("L");
				String brand_name = jsonObj.getString("N");
				JSONArray contentArr = jsonObj.getJSONArray("List");
				for (int j = 0; j < contentArr.length(); j++) {
					JSONObject itemObj = contentArr.getJSONObject(j);
					int carfactory_id = itemObj.getInt("I");
					String carfactoty_name = itemObj.getString("N");
					JSONArray itemArr = itemObj.getJSONArray("List");
					for (int k = 0; k < itemArr.length(); k++) {
						JSONObject modelObj = itemArr.getJSONObject(k);
						int model_id = modelObj.getInt("I");
						String model_name = modelObj.getString("N");
						AutoHomeCarInfo carinfo = new AutoHomeCarInfo(brand_id, brand_name, brand_firstletter,
								carfactory_id, carfactoty_name, model_id, model_name, 0, 0);
						carinfodao.insertData(carinfo);
						System.out.println(carinfo);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: crawlContent
	 * 
	 * @Description:
	 * 
	 * @param url
	 * 
	 * @see com.zizo.core.CrawlInterface#crawlContent(java.lang.String)
	 */
	@Override
	public void crawlContent(String url) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: run
	 * 
	 * @Description:
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {

	}

	public static void main(String[] args) {
		Connection currConn = ConnectionPool.getConnection("autohome_other", "localhost", "3306");
		CarInfoDaoOfAutoHome dao = new CarInfoDaoOfAutoHome(currConn);
		CarInfoCrawlOfAutoHome carinfo = new CarInfoCrawlOfAutoHome("", null, null, dao);
		carinfo.getItems("http://car.autohome.com.cn/javascript/NewSpecCompare.js?");
	}
}
